package com.zx.map.service.impl;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Component;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;

import com.zx.map.dao.mapper.MerchantInfoMapper;
import com.zx.map.dao.mapper.StoreInfoMapper;
import com.zx.map.dao.mapper.TodoTaskMapper;
import com.zx.map.dao.model.MerchantInfo;
import com.zx.map.dao.model.StoreInfo;
import com.zx.map.dao.model.StoreInfoExample;
import com.zx.map.dao.model.TodoTask;
import com.zx.map.service.MerchantInfoService;
import com.zx.map.service.StoreInfoService;
import com.zx.map.service.TodoTaskService;
import com.zx.map.service.model.MerchantInfoModel;
import com.zx.map.service.model.MerchantInfoTodoTask;
import com.zx.map.service.model.TodoTaskModel;
import com.zx.map.utils.MyBeanUtils;
import com.zx.mobile.MobileCommonHelper;
@Component("storeInfoService")
public class StoreInfoServiceImpl implements StoreInfoService {

	@Autowired
	private StoreInfoMapper mapper;
	@Autowired
    private TodoTaskMapper tmapper;
	@Autowired
	private MerchantInfoMapper mmapper; 
	@Autowired
	private TransactionTemplate transactionTemplate;
	@Autowired
	private MerchantInfoService merchantInfoService;
	@Autowired
	private TodoTaskService todoTaskService;
	@Override
	public int add(StoreInfo storeInfo) throws DataAccessException {
//		int returnCode = (int) transactionTemplate.execute(new TransactionCallback<Object>() {
//
//			@Override
//			public Object doInTransaction(TransactionStatus status) {
//				int result = mapper.insertSelective(storeInfo);
//				
////				MerchantInfo merchant = mmapper.selectByPrimaryKey(storeInfo.getMerchantId());
////				merchant.setUpdateDate(new Date());
////				merchant.setStatus(MobileCommonHelper.APPUSERSTATUS_WAIT);
////				mmapper.updateByPrimaryKeySelective(merchant);
//				
////				addTodoTask(storeInfo, merchant);
//				
//				return result;
//			}
//			
//		});
		mapper.insertSelective(storeInfo);
		return 1;
	}

	@Override
	public void update(StoreInfo storeInfo) throws DataAccessException {
		mapper.updateByPrimaryKeySelective(storeInfo);

	}

	@Override
	public StoreInfo find(String merchantId) throws DataAccessException {
		StoreInfoExample example = new StoreInfoExample();
		StoreInfoExample.Criteria criteria = example.createCriteria();
		criteria.andMerchantIdEqualTo(merchantId);
		List<StoreInfo> list = mapper.selectByExample(example);
		return list.isEmpty() ? null : list.get(0);
	}

	private int addTodoTask(StoreInfo storeInfo, MerchantInfo merchantInfo) {
		Integer taskId = 0;
		try {
			
			MerchantInfoTodoTask todoTask = new MerchantInfoTodoTask();
			
			MerchantInfoModel merchantModel = merchantInfoService.getModelByKey(merchantInfo.getMerchantId());
			
			TodoTaskModel todoTaskModel = new TodoTaskModel();
			todoTaskModel.setApplyOperator(merchantModel.getTelephone());
			todoTaskModel.setApproveStatus(MobileCommonHelper.APPROVESTATUS_WAIT);
//	        todoTaskModel.setApplyType("");
	        todoTaskModel.setApproveStatus(MobileCommonHelper.APPROVESTATUS_WAIT);
	        todoTaskModel.setTaskType(MobileCommonHelper.TASKTYPE_MERCHANTADD);  ////新增商户
	        todoTaskModel.setTaskOperatorType(MobileCommonHelper.TASKOPERATORTYPE_UPDATE);//修改
	        todoTaskModel.setTaskKey(merchantInfo.getMerchantId());//主键信息
	        todoTaskModel.setCreateDate(new Date());
	        todoTaskModel.setUpdateDate(new Date());
	        todoTask.setTodoTaskModel(todoTaskModel);
	        
			todoTask.setMerchantInfoModel(merchantModel);
			taskId = todoTaskService.addTodoTask(todoTask, todoTask.getTodoTaskModel());
		} catch (DataAccessException | IOException e) {
			e.printStackTrace();
		}
		return taskId;
	}

	@Override
	public List<StoreInfo> getStoreInfoByIdentityNo(String identityNo) throws DataAccessException {
		StoreInfoExample example = new StoreInfoExample();
		StoreInfoExample.Criteria criteria = example.createCriteria();
		criteria.andIdentifyNoEqualTo(identityNo);
		return mapper.selectByExample(example);
	}
}
